<?php

namespace App\Http\Controllers\Wechat;

use App\Libraries\classs\Face;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\View\View;
use Redirect, Input, Response;
use App\Libraries\classs\Upload;
use App\Libraries\classs\red\Send;
use App\Models\Shares\Red\Logs as Red;
class ShareController extends BaseController
{
    public function index(){
//        echo "页面维护!请稍后访问";
//        exit();
        return view('wechat.share.index');
    }

    public function upload(Request $request){
        exit();
        $base64=$request->get('base64');
        $count = Face::getInstance()->getFaceCount($this->_uid,2);
        if($count > 0){
            return Response::json(array('success'=>false,'msg'=>"您已经上传过图片了!"));
        }
        if($base64){
            $upload_rs=Upload::getInstance()->upload_base64($base64);
            if($upload_rs['success']) {
                $face_rs = Face::getInstance()->createFace($this->_uid,$upload_rs['id'],2);
                if($face_rs){
                    $red = $this->send();
                    return $red;
//                    return Response::json(array('success'=>true));
                }
                return Response::json(array('success'=>false,'msg'=>"上传的不是人脸!请重新上传图片!"));
            }
        }
        return Response::json(array('success'=>false,'msg'=>"上传错误!请重新上传图片!"));
    }

    public function send(){
        exit();
        $redlog = Red::where('x_uid','=',$this->_uid)->where('type','=',1)->first();
        if($redlog){
            return Response::json(array('success'=>false,'msg'=>"您已经领取过红包了,赶紧分享给其他伙伴,领取红包吧!"));
        }
        $count = Face::getInstance()->getFaceCount($this->_uid,2);
        if($count > 0){
            $cash = 100;
            $luckyMoneyData = [
                'send_name'        => '百万民选',
                're_openid'        => $this->_user->openid,
                'total_num'        => 1,  //固定为1，可不传
                'total_amount'     => $cash,  //单位为分，不小于300
                'wishing'          => '恭喜发财',
//           'client_ip'        => '192.168.0.1',  //可不传，不传则由 SDK 取当前客户端 IP
                'act_name'         => '百万民选',
                'remark'           => '百万民选',
            ];
            $rs = Send::getInstance()->sendNormal($luckyMoneyData);
            if($rs['result_code'] == "SUCCESS"){
                $red_data =array(
                    'x_uid'=>$this->_uid,
                    'red'=>$cash,
                    'type'=>1,
                );
                Red::create($red_data);
                return Response::json(array('success'=>true,'msg'=>"恭喜你！领取红包成功！"));
            }
            return Response::json(array('success'=>false,'msg'=>$rs));
//            return Response::json(array('success'=>false,'msg'=>'很遗憾，你来晚了，红包已发完~'));
        }
        return Response::json(array('success'=>false,'msg'=>"还没上传表情!"));
    }


    public function test(){
        $cash = 100;
        $luckyMoneyData = [
            'mch_billno'       => 'xy123456',
            'send_name'        => '测试红包',
            're_openid'        => $this->_user->openid,
            'total_num'        => 1,  //固定为1，可不传
            'total_amount'     => $cash,  //单位为分，不小于300
            'wishing'          => '祝福语',
//           'client_ip'        => '192.168.0.1',  //可不传，不传则由 SDK 取当前客户端 IP
            'act_name'         => '测试活动',
            'remark'           => '测试备注',
        ];
        $rs = Send::getInstance()->sendNormal($luckyMoneyData);
        print_r($rs);
    }
}
